package com.zky.gulimall.coupon.service.impl;

import com.zky.common.to.MemberPrice;
import com.zky.common.to.SkuReductionTo;
import com.zky.gulimall.coupon.entity.MemberPriceEntity;
import com.zky.gulimall.coupon.entity.SkuLadderEntity;
import com.zky.gulimall.coupon.service.MemberPriceService;
import com.zky.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zky.common.utils.PageUtils;
import com.zky.common.utils.Query;

import com.zky.gulimall.coupon.dao.SkuFullReductionDao;
import com.zky.gulimall.coupon.entity.SkuFullReductionEntity;
import com.zky.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.util.CollectionUtils;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {

        if(skuReductionTo.getFullCount()>0){
            //1 保存满减打折，会员价
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
            skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
            skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }



        if(skuReductionTo.getFullPrice().compareTo(new BigDecimal(0))==1){
            SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo,reductionEntity);
            this.save(reductionEntity);
        }


        //会员价
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if(CollectionUtils.isEmpty(memberPrice)){
            log.warn("memberPrice is empty");
            return;
        }
        List<MemberPriceEntity> entityList = memberPrice.stream().map(e -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(e.getId());
            memberPriceEntity.setMemberLevelName(e.getName());
            memberPriceEntity.setMemberPrice(e.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->item.getMemberPrice().compareTo(new BigDecimal(0))==1).collect(Collectors.toList());
        memberPriceService.saveBatch(entityList);
    }

}